//
// Created by steven on 2017/3/10.
// Copyright (c) 2017 steven. All rights reserved.
//

#import "GMDebtorDataController.h"
#import "GMDebtorModel.h"


@implementation GMDebtorDataController

- (void)requestDebtorsWithParams:(NSDictionary *)params callback:(GMCompletionCallback)callback {
    [self requestWithType:GET url:ACTION_GUARD_DEBTORS params:params success:^(id object) {
        NSArray *data = object[@"data"];
        NSMutableArray *tempArray = [[NSMutableArray alloc] init];

        [data enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            GMDebtorModel *debtorModel = [[GMDebtorModel alloc] init];
            debtorModel.id = [obj[@"id"] integerValue];
            debtorModel.log = [obj[@"log"] floatValue];
            debtorModel.lat = [obj[@"lat"] floatValue];

            [tempArray addObject:debtorModel];
        }];

        [self.debtors removeAllObjects];
        [self.debtors addObjectsFromArray:tempArray];

        callback(nil);
    } failure:^(NSInteger statusCode, NSString *message) {
        NSString *errorString = nil;
        switch (statusCode) {
            case 400:
            case 401:
            case 403:
            case 404:
                errorString = @"获取信息失败";
                break;
            case SERVER_NETWORK_OFF:
                errorString = @"服务器错误";
                break;
            case USER_NETWORK_OFF:
                errorString = @"请检查手机网络";
                break;
            default:
                errorString = UNKOWN_ERROR;
                break;
        }
        callback(message ? message : errorString);
    }];
}

- (void)requestDetailDebtorWithParams:(NSDictionary *)params callback:(GMCompletionCallback)callback {
    [self requestWithType:GET url:ACTION_GUARD_DEBTOR params:params success:^(id object) {
        NSDictionary *data = object[@"data"];

        self.debtorModel = [[GMDebtorModel alloc] init];
        self.debtorModel.id = [data[@"id"] integerValue];
        self.debtorModel.name = data[@"name"];
        self.debtorModel.phoneNumber = data[@"phone_number"];
        self.debtorModel.startTime = data[@"start_time"];
        self.debtorModel.endTime = data[@"end_time"];
        self.debtorModel.status = data[@"status"];
        self.debtorModel.pledge = data[@"pledge"];
        self.debtorModel.lat = [data[@"lat"] floatValue];
        self.debtorModel.log = [data[@"log"] floatValue];

        callback(nil);
    } failure:^(NSInteger statusCode, NSString *message) {
        NSString *errorString = nil;
        switch (statusCode) {
            case 400:
            case 401:
            case 403:
            case 404:
                errorString = @"获取信息失败";
                break;
            case SERVER_NETWORK_OFF:
                errorString = @"服务器错误";
                break;
            case USER_NETWORK_OFF:
                errorString = @"请检查手机网络";
                break;
            default:
                errorString = UNKOWN_ERROR;
                break;
        }
        callback(message ? message : errorString);
    }];
}

- (void)updateDebtorWithParams:(NSDictionary *)params callback:(GMCompletionCallback)callback {
    [self requestWithType:POST url:ACTION_UPDATE_DEBTOR params:params success:^(id object) {
        NSDictionary *data = object[@"data"];
        self.debtorModel.name = data[@"name"];
        self.debtorModel.phoneNumber = data[@"phone_number"];
        self.debtorModel.pledge = data[@"pledge"];

        callback(nil);
    } failure:^(NSInteger statusCode, NSString *message) {
        NSString *errorString = nil;
        switch (statusCode) {
            case 400:
            case 401:
            case 403:
            case 404:
                errorString = @"获取信息失败";
                break;
            case SERVER_NETWORK_OFF:
                errorString = @"服务器错误";
                break;
            case USER_NETWORK_OFF:
                errorString = @"请检查手机网络";
                break;
            default:
                errorString = UNKOWN_ERROR;
                break;
        }
        callback(message ? message : errorString);
    }];
}

- (void)controlDebtorWithParams:(NSDictionary *)params callback:(GMCompletionCallback)callback {
    [self requestWithType:POST url:ACTION_CONTROL_DEBTOR params:params success:^(id object) {
        callback(nil);
    } failure:^(NSInteger statusCode, NSString *message) {
        NSString *errorString = nil;
        switch (statusCode) {
            case 400:
            case 401:
            case 403:
            case 404:
                errorString = @"获取信息失败";
                break;
            case SERVER_NETWORK_OFF:
                errorString = @"服务器错误";
                break;
            case USER_NETWORK_OFF:
                errorString = @"请检查手机网络";
                break;
            default:
                errorString = UNKOWN_ERROR;
                break;
        }
        callback(message ? message : errorString);
    }];
}

- (NSMutableArray *)debtors {
    if (!_debtors) {
        _debtors = [[NSMutableArray alloc] init];
    }

    return _debtors;
}

@end